package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itheima.reggie.bean.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
//        先判断添加的是套餐还是菜品 套餐没有菜品口味选择和菜品id
//        购物车的id 由登录用户中取得

//        查询购物车是否存在 存在再数量买那里加1，不存在就是为1并更修 修改时间

        LambdaQueryWrapper<ShoppingCart> sw = new LambdaQueryWrapper<>();


        //        再判断菜品是否为空，为空证明添加的是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            sw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

        } else {
            sw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        sw.eq(ShoppingCart::getUserId, BaseContext.get());
        ShoppingCart selectOne = shoppingCartMapper.selectOne(sw);

        if (selectOne != null) {

            Integer number = selectOne.getNumber();

            selectOne.setNumber(number + 1);
            shoppingCartMapper.updateById(selectOne);

            return selectOne;

        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.get());

            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
            return shoppingCart;
        }

    }

    @Override
    public  List<ShoppingCart> list() {
        LambdaQueryWrapper<ShoppingCart> sw = new LambdaQueryWrapper<>();

        sw.eq(ShoppingCart::getUserId, BaseContext.get());
        sw.orderByDesc(ShoppingCart::getCreateTime);


        return shoppingCartMapper.selectList(sw);
    }

    @Override
    public int delete() {
        LambdaQueryWrapper<ShoppingCart> sw = new LambdaQueryWrapper<>();
        sw.eq(ShoppingCart::getUserId, BaseContext.get());

        return shoppingCartMapper.delete(sw);
    }



    @Override
    public ShoppingCart deleteList(ShoppingCart shoppingCart) {
        //判断购物车的是什么数据，如果是菜品存在就删除菜品，如果是套餐就删除套餐
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

//判断走的时候什么数据

        if (shoppingCart.getDishId()!= null) {
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else{
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }
        qw.eq(ShoppingCart::getUserId,BaseContext.get());


        ShoppingCart selectOne = shoppingCartMapper.selectOne(qw);


                selectOne.setNumber(selectOne.getNumber()-1);

                shoppingCartMapper.updateById(selectOne);

            if (selectOne.getNumber()==0){
            shoppingCartMapper.deleteById(selectOne.getId());
        }


        return selectOne;
    }
}
